//
//  GoodsDetailsViewController.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/20.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "GoodsDetailsViewController.h"

#import "GoodsDetailFooterView.h"
#import "GoodsDetailsHeaderView.h"
#import "GoodsSpecificationsCell.h"
#import "GoodsEvaluationCell.h"
#import "GoodsStoreCell.h"
#import "GoodsDetailModel.h"
#import "GoodsCommentModel.h"
#import "GoodContentCell.h"
#import "GoodDetailTopSuckView.h"
#import "GoodSkusOptionBoardView.h"
#import "GoodBottomMenuView.h"

#import "ShoppingCartTool.h"

#import "SecondShoppingCartVC.h"

#import "ConfirmOrderViewController.h"

@interface GoodsDetailsViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@property (nonatomic,strong) GoodDetailTopSuckView *topSuckV;

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) GoodsDetailsHeaderView *headerView;
@property (nonatomic,strong) GoodBottomMenuView *bottomMenuV;
@property (nonatomic,strong) GoodsDetailFooterView *footerView;


@property (nonatomic,assign) float goodsSpecCellHeight;//商品规格cell高度

@property (nonatomic,assign) float goodsEvaluationCellHeight;//商品评价cell高度

@property (nonatomic,assign) float goodsContentCellHeight;//商品内容高度
///当前商品model
@property (nonatomic,strong) GoodsDetailModel *currentGoodsModel;
///当前选中商品规格model
@property (nonatomic,strong) GoodsSkuModel *currentSkuModel;

///购物车中商品数量
@property (nonatomic,assign) NSInteger currentCartnum;


@end

@implementation GoodsDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topTitle = @"商品详情";
        
    self.goodsSpecCellHeight = 170;
    self.goodsEvaluationCellHeight = 100;
    self.goodsContentCellHeight = 200;
    [self initUI];
    [self viewBlocks];//block 回调处
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self getGoodsDetailWithID:self.goodsID];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self getGoodsDetailWithID:self.goodsID];
}
#pragma mark - setter
- (void)setCurrentSkuModel:(GoodsSkuModel *)currentSkuModel{
    _currentSkuModel = currentSkuModel;
    self.headerView.currentSkuModel = _currentSkuModel;
    [self.tableView reloadData];
}
#pragma mark - data
-(void)getGoodsDetailWithID:(NSInteger)goodsID{
    [NetWorkingShop getGoodsDetailWithID:goodsID WithSuccessBlock:^(id  _Nonnull responseObj) {
        NSDictionary *data = responseObj[@"data"];
        GoodsDetailModel *model = [GoodsDetailModel yy_modelWithDictionary:data];
        model.ID = [data[@"id"] integerValue];
        NSArray *skus = data[@"skus"];
        NSMutableArray *muSkuArr = [NSMutableArray array];
        for (NSDictionary *dic in skus) {
            GoodsSkuModel *skuM = [GoodsSkuModel paraWithDict:dic];
            [muSkuArr addObject:skuM];
        }
        model.skus = (NSArray *)muSkuArr;
        NSMutableArray *comments = [NSMutableArray array];
        NSArray *arr = data[@"comments"];
        for (NSDictionary *dic in arr) {
            GoodsCommentModel *commentM = [GoodsCommentModel paraWithDict:dic withShowDetail:NO];
            [comments addObject:commentM];
        }
        model.comments = comments;
        self.currentGoodsModel = model;
        self.headerView.model = self.currentGoodsModel;
        self.footerView.goodContent = self.currentGoodsModel.content;
        self.currentSkuModel = model.skus.count > 0 ? model.skus[0]:[GoodsSkuModel new];
        [self.tableView reloadData];
        
        self.currentCartnum = [data[@"cartnum"] integerValue];
        self.bottomMenuV.cartNum = self.currentCartnum;
    }];
}
#pragma mark - UI
-(void)initUI{
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.topSuckV];
    [self.view addSubview:self.bottomMenuV];
}
//block 统一回调处
-(void)viewBlocks{
    WEAKSELF(self);
    [self.topSuckV setTouchBlock:^(NSInteger touchIndex) {
        if (touchIndex == 0) {
            weakSelf.topSuckV.alpha = 0;
            [weakSelf.tableView setContentOffset:CGPointZero animated:NO];
        }else if (touchIndex == 1) {
            NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:0];
            [weakSelf.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionNone animated:YES];
        }else{
            [weakSelf.tableView setContentOffset:CGPointMake(0, self.tableView.contentSize.height - self.footerView.height+50) animated:YES];
        }
    }];
    //底部菜单blocks
    [self.bottomMenuV setTouchBlock:^(NSInteger type) {
        switch (type) {
            case 0://店铺
                [Configuration judgeLoginStatus];//判断是否登录
                break;
            case 1://客服
                [Configuration judgeLoginStatus];
                break;
            case 2://购物车
                [Configuration judgeLoginStatus];
                [weakSelf toShoppingCartClick];
                break;
            case 3://加入购物车
                [weakSelf showChooseSkusViewWithType:1];
                break;
            case 4://立即下单
                [weakSelf showChooseSkusViewWithType:2];
                break;
            default:
                break;
        }
    }];
}
#pragma mark - lazy
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kHeight_NavBar, ScreenWidth, ScreenHeight-kHeight_NavBar-kSafe_Bottom-60) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = UIColor.whiteColor;
        [_tableView setTableHeaderView:[self tableHeaderView]];
        [_tableView setTableFooterView:[self tableFooterView]];
    }
    return _tableView;
}
//表头
- (GoodsDetailsHeaderView *)tableHeaderView{
    WEAKSELF(self);
    self.headerView = [[GoodsDetailsHeaderView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth+150)];
    [self.headerView setUpdateHeightBlock:^(CGFloat height) {
        weakSelf.headerView.height = height;
    }];
    return self.headerView;
}
- (GoodsDetailFooterView *)tableFooterView{
    WEAKSELF(self);
    self.footerView = [[GoodsDetailFooterView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 200)];
    [self.headerView setUpdateHeightBlock:^(CGFloat height) {
        weakSelf.footerView.height = height;
        weakSelf.goodsContentCellHeight = height;
    }];
    return self.footerView;
}
//吸顶栏目view
- (GoodDetailTopSuckView *)topSuckV{
    if (!_topSuckV) {
        _topSuckV = [[GoodDetailTopSuckView alloc] initWithFrame:CGRectMake(0, kHeight_NavBar, ScreenWidth, 55)];
        _topSuckV.alpha = 0;
    }
    return _topSuckV;
}
//底部菜单栏
- (GoodBottomMenuView *)bottomMenuV{
    if (!_bottomMenuV) {
        _bottomMenuV = [[GoodBottomMenuView alloc] initWithFrame:CGRectMake(0, ScreenHeight-60-kSafe_Bottom, ScreenWidth, 60+kSafe_Bottom)];
    }
    return _bottomMenuV;
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return self.goodsSpecCellHeight;
    }else if (indexPath.row == 1) {
        return self.goodsEvaluationCellHeight;
    }else{
        return 60;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        GoodsSpecificationsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsSpecificationsCell"];
        if (!cell) {
            cell = [[GoodsSpecificationsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GoodsSpecificationsCell"];
        }
        if (self.currentGoodsModel) {
            cell.model = self.currentGoodsModel;
        }
        cell.currentSkuModel = self.currentSkuModel;
        [cell setUpdateCellHeightBlock:^(CGFloat cellHeight) {
            self.goodsSpecCellHeight = cellHeight;
            [self.tableView beginUpdates];
            [self.tableView endUpdates];
        }];
        [cell setChooseGoodSkuBlock:^{
            //选择规格
            [self showChooseSkusViewWithType:0];
        }];
        return cell;
    }else if (indexPath.row == 1) {
        GoodsEvaluationCell *evaCell = [tableView dequeueReusableCellWithIdentifier:@"GoodsEvaluationCell"];
        if (!evaCell) {
            evaCell = [[GoodsEvaluationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GoodsEvaluationCell"];
        }
        evaCell.model = self.currentGoodsModel;
        [evaCell setUpdateCellHeightBlock:^(CGFloat cellHeight) {
            self.goodsEvaluationCellHeight = cellHeight;
            [self.tableView beginUpdates];
            [self.tableView endUpdates];
        }];
        return evaCell;
    }else {
        GoodsStoreCell *storeCell = [tableView dequeueReusableCellWithIdentifier:@"GoodsStoreCell"];
        if (!storeCell) {
            storeCell = [[GoodsStoreCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GoodsStoreCell"];
        }
        storeCell.model = self.currentGoodsModel;
        [storeCell setToStoreBlock:^{
            //去商店
        }];
        return storeCell;
    }
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y > kHeight_NavBar) {
        self.topSuckV.alpha = 1;
    }else if (scrollView.contentOffset.y <= 0 ){
        self.topSuckV.alpha = 0;
    }else{
        self.topSuckV.alpha = scrollView.contentOffset.y/kHeight_NavBar;
    }
}
#pragma mark - SEL
//选择商品规格   type   0 选择规格     1 加入购物车     2 立即下单
-(void)showChooseSkusViewWithType:(NSInteger)type{
    WEAKSELF(self);
    GoodSkusOptionBoardView *view = [[GoodSkusOptionBoardView alloc] initWithSkus:self.currentGoodsModel.skus];
    [view show];
    [view setChooseSkuModelBlock:^(GoodsSkuModel * _Nonnull model) {
        weakSelf.currentSkuModel = model;
        if (type == 0) {
        }else if (type == 1) {
            [weakSelf addToCartClick];
        }else{
            [weakSelf placeOrderClick];
        }
    }];
}
//商品加入购物车
-(void)addToCartClick{
    if ([Configuration getLoginStatus] == NO) {
        [Configuration loginClick];
        return;
    }else{
        CGPoint point = [self.bottomMenuV.addToCartBtn convertPoint:CGPointMake(self.bottomMenuV.addToCartBtn.width/2, self.bottomMenuV.addToCartBtn.height/2) toView:self.view];
        CGPoint point2 = [self.bottomMenuV.cartBtn convertPoint:CGPointMake(self.bottomMenuV.cartBtn.width/2, self.bottomMenuV.cartBtn.height/2) toView:self.view];
        [NetWorkingCart addToCart:self.currentGoodsModel.ID skuId:self.currentSkuModel.ID withSuccessBlock:^(id  _Nonnull responseObj) {
            //加入购物车动画
            [ShoppingCartTool addToShoppingCartWithGoodsImage:[self goodsImage] startPoint:point endPoint:point2 completion:^(BOOL finished) {
                [self getGoodsDetailWithID:self.goodsID];
            }];
        }];
    }
}
//商品+1 图片
-(UIImage *)goodsImage{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    label.backgroundColor = RGB(71, 108, 255);
    label.textColor = UIColor.whiteColor;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = FontSet(15);
    label.text = @"+1";
    label.clipsToBounds = YES;
    label.layer.cornerRadius = 15;
    return [label getViewImage];
}
//商品下单
-(void)placeOrderClick{
    if ([Configuration getLoginStatus] == NO) {
        [Configuration loginClick];
        return;
    }
    ConfirmOrderViewController *vc = [[ConfirmOrderViewController alloc] init];
    vc.module = @"product";
    vc.skuId = self.currentSkuModel.ID;
    [self.navigationController pushViewController:vc animated:YES];
}

//跳转购物车页面
-(void)toShoppingCartClick{
    SecondShoppingCartVC *vc = [[SecondShoppingCartVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}



@end


